package actions;

import java.sql.SQLException;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.RowSet;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import db.DbException;
import forms.DegreeForm;

public class DegreeDisciplineAction extends Action {

	private static final boolean DEBUG = true;
	//private static final int UNKNOWN = -1;
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws DbException {
	
		DegreeForm dform = (DegreeForm) form;
		
		String major = dform.getMajor();
		String month = dform.getMonth();
		String year = dform.getYear();
		String gpa = dform.getGpa();
		String title = dform.getTitle();
		
		if (DEBUG) {
			System.err.println("DegreeDisciplineAction: saving major(" + major +")");
			System.err.println("DegreeDisciplineAction: saving month(" + month +")");
			System.err.println("DegreeDisciplineAction: saving year(" + year +")");
			System.err.println("DegreeDisciplineAction: saving gpa(" + gpa +")");
			System.err.println("DegreeDisciplineAction: saving title(" + title +")");
		}
		
		// store DegreeForm into session
		dform = (DegreeForm)request.getSession().getAttribute("dform");
		dform.setMajor(major);
		dform.setMonth(month);
		dform.setYear(year);
		dform.setGpa(gpa);
		dform.setTitle(title);
		
		if (DEBUG) {
			System.err.println("DegreeDisciplineAction: form location(" + dform.getLocation() + ")");
			System.err.println("DegreeDisciplineAction: form university(" + dform.getUniversity() + ")");
			System.err.println("DegreeDisciplineAction: form major(" + dform.getMajor() + ")");
			System.err.println("DegreeDisciplineAction: form month(" + dform.getMonth() + ")");
			System.err.println("DegreeDisciplineAction: form year(" + dform.getYear() + ")");
			System.err.println("DegreeDisciplineAction: form gpa(" + dform.getGpa() + ")");
			System.err.println("DegreeDisciplineAction: form title(" + dform.getTitle() + ")");
		}
		
		int degrees_count = (Integer)request.getSession().getAttribute("degrees_count");
		Vector<DegreeForm> degrees = (Vector<DegreeForm>)request.getSession().getAttribute("degrees");
		
		if (degrees == null || degrees_count > degrees.size()) {
			
			if (DEBUG) {
				System.err.println("DegreeDisciplineAction: creating new degrees");
			}
			
			if (degrees == null || degrees_count == 1) {
				degrees = new Vector<DegreeForm>();
				request.getSession().setAttribute("degrees", degrees);
			}
			
			degrees.add(dform);
		}
 		
		return mapping.findForward("success");
	}
	
}
